<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>项目管理</el-breadcrumb-item>
      <el-breadcrumb-item>礼包管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="queryInfo.query"
        size="mini"
      >
        <el-form-item label="项目名称【ID】" prop="project_id">
          <el-select
            v-model="queryInfo.query.project_id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.get_project"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="礼包类型" prop="type">
          <el-select
            v-model="queryInfo.query.type"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.gift_type"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="礼包名称【ID】" prop="id">
          <el-select
            v-model="queryInfo.query.id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.gift"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryInfo.query.status"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.gift_status"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作区域 -->
      <div>
        <el-button
          class="theme-background-color"
          icon="el-icon-plus"
          size="mini"
          @click="$router.push('/gift/AddGift')"
        >
          新增礼包
        </el-button>
        <el-button
          icon="el-icon-s-promotion"
          size="mini"
          @click="batchPutGift = true"
        >
          批量发放礼包
        </el-button>
      </div>

      <!-- 用户列表区域 -->
      <el-table :data="list" border stripe>
        <el-table-column
          label="礼包名称【ID】"
          min-width="150px"
          max-width="300px"
        >
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="toDetail(scope.row.id, scope.row.name)"
              >{{ scope.row.name }}【{{ scope.row.id }}】</el-link
            >
          </template>
        </el-table-column>
        <el-table-column label="礼包类型" prop="type.name"></el-table-column>
        <el-table-column
          label="所属项目【ID】"
          prop="project_name"
        ></el-table-column>
        <el-table-column label="状态" prop="status.name"></el-table-column>
        <el-table-column label="创建人" prop="creaor_name"></el-table-column>
        <el-table-column label="创建时间" prop="create_time"></el-table-column>
        <el-table-column label="备注" prop="remark"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!--关闭状态：开启操作-->
            <el-link
              type="primary"
              v-if="scope.row.status.id == 2"
              @click="enable(scope.row)"
            >
              开启
            </el-link>
            <!--开启状态：关闭操作、发放操作-->
            <el-link
              type="danger"
              v-if="scope.row.status.id == 1"
              @click="disable(scope.row)"
            >
              关闭
            </el-link>
            &nbsp;
            <el-link
              v-if="scope.row.status.id == 1"
              type="primary"
              @click="putoff(scope.row)"
            >
              发放
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!--批量发放礼包弹框：待实现-->
    <el-dialog :visible.sync="batchPutGift" title="批量发放礼包">
      <el-form>
        <el-form-item label="导入文件" :label-width="formLabelWidth">
          <div class="row_ss">
            <el-upload
              ref="upload"
              class="upload-demo"
              action="https://jsonplaceholder.typicode.com/posts/"
              :auto-upload="false"
            >
              <!--
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                multiple
                :limit="3"
                :on-exceed="handleExceed"
                :file-list="fileList" 
                :on-change="handleChange"
              -->
              <div class="row_ss">
                <el-input :model="fileData" autocomplete="off"></el-input>
                <el-button
                  class="theme-background-color"
                  size="mini"
                  style="margin-left: 1rem"
                  >选择文件</el-button
                >
              </div>
              <div
                class="row_ss"
                style="align-items: center; margin-top: 0.8rem"
              >
                <el-button
                  class="theme-background-color"
                  size="mini"
                  @click="submitUpload"
                  >开始上传</el-button
                >
                <span style="margin-left: 1rem"
                  >待处理数据：<label>0</label></span
                >
              </div>
            </el-upload>

            <el-button size="mini" style="margin-left: 1rem">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: {
          project_id: '',
          type: '',
          id: '',
          status: ''
        },
        page: 1,
        size: 20
      },
      selectData: [],
      list: [],
      total: 0,
      btnLoading: false,

      batchPutGift: false,
      formLabelWidth: '120px',
      fileData: {}
    }
  },
  created() {
    this.getSelectData()
    this.getListData()
  },
  methods: {
    async getSelectData() {
      //获取筛选条件数据：
      const { data: res } = await this.$http.post('/admin/data/find', {
        code: 'get_project,gift_type,gift,gift_status'
      })
      // 返回结果
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    searchList() {
      this.queryInfo.page = 1
      this.getListData()
    },
    resetSearchForm() {
      //重置筛选条件数据：
      //   console.log(this)
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    async getListData() {
      //获取列表数据：
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/gift/list',
        this.queryInfo
      )
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    async disable(row) {
      // 弹框询问是否禁用礼包
      this.$prompt(
        '是否确认关闭礼包' + row.name + '【' + row.id + '】，关闭后不可使用',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          inputPattern: /^(?!.{500}|^\s*$)/g,
          inputErrorMessage: '请输入关闭原因',
          closeOnClickModal: false
        }
      )
        .then(async ({ value }) => {
          console.log(value)
          const { data: res } = await this.$http.post(
            '/admin/gift/activeGift',
            {
              id: row.id,
              reason: value,
              active: 'close'
            }
          )
          if (res.meta.status !== 200) {
            return this.$message.error(res.meta.msg)
          }
          this.$message.success(res.meta.msg)
          this.getListData()
        })
        .catch(() => {
          //  this.$message.info('已取消操作')
        })
    },
    async enable(row) {
      // 弹框询问是否启用礼包
      const confirmResult = await this.$confirm('确定开启该礼包？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消操作')
      }
      const { data: res } = await this.$http.post('/admin/gift/activeGift', {
        //开启礼包
        id: row.id,
        active: 'active'
      })
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getListData()
    },
    // 礼包订单详情
    toDetail(id, name) {
      const newpage = this.$router.resolve({
        path: '/gift/GiftDetail',
        query: { id: id }
        //query: { id:id,name:name }
      })
      window.open(newpage.href, '_blank')
    },

    submitUpload() {
      //上传文件：
      this.$refs.upload.submit()
    }
  }
}
</script>

<style lang="less" scoped>
/*
.el-form-item__content{
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}*/

.row_ss {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: flex-start;
}
</style>
